
var stage;
var target;

/* function init */
function init(){
    stage = new createjs.Stage("canvas");
    createjs.Ticker.useRAF = true; //use requestAnimationFrame(); <-- more efficient animations
           
    new mob("images/move.png",100,50); /*mob() returns a object 'character'*/

    var spriteSheet2 = new load_spriteSheet("images/move.png", 32, 32);
    while(!spriteSheet2){/*wait until the spriteSheet is loaded*/}
    var animations2 = {
                run_left:{frames: [15,17], next:"stand_left", frequency:2},
                stand_left:16,
                run_right:{frames: [18,20], next:"stand_right", frequency:2},
                stand_right:19,
                run_up:{frames: [21,23], next:"stand_up", frequency:2},
                stand_up:22,
                run_down:{frames: [12,14], next:"stand_down", frequency:2},
                stand_down:13
    };
    character = new char(spriteSheet2.init(animations2), 100, 100); /*en el metodo init() del spriteSheet le añadimos las animaciones*/    
    target = new move_listener(character,'w','s','a','d');
    createjs.Ticker.addEventListener("tick", tick);
    
}
/* end */





/* Frame */
var count = 0;
function tick(){
    count++;
    if(count===4){
        stage.update();
        count = 0;
        target.move(last_key);
    }
}
/* end */


/* class char */
/*
 * @method animation(string)
 * @method move(x,y)
 * @method get_x()
 * @method get_y()
 */
function char(spriteSheet, x, y){
    var bmpSeq;
    bmpSeq = new createjs.BitmapAnimation(spriteSheet);
    bmpSeq.x = x;
    bmpSeq.y = y;
    stage.addChild(bmpSeq);
    bmpSeq.gotoAndPlay("stand_down");
    this.animation = function(str){bmpSeq.gotoAndPlay(str);};
    this.move = function(tx,ty){
        if(bmpSeq.x < tx){
            while(bmpSeq.x < tx)
            bmpSeq.x++;
        }else
        if(bmpSeq.x > tx){
            while(bmpSeq.x > tx)
            bmpSeq.x--;
        }else
        if(bmpSeq.y < ty){
            while(bmpSeq.y < ty)
            bmpSeq.y++;
        }else
        if(bmpSeq.y > ty){
            while(bmpSeq.y > ty)
            bmpSeq.y--;
        }
    };
    this.get_x = function(){return bmpSeq.x;};
    this.get_y = function(){return bmpSeq.y;};
    return this;
}

/* end */


/* class load_spriteSheet */
/*
 * @method init(animations)
 * @variable data
 */
function load_spriteSheet(str, width, height){
    var imagen = new Image();
    this.data;
    imagen.src = str;
    this.init = function(anim){
        data = {
                    images: [imagen],
                    frames: {width:width, height:height, regX:width/2, regY:height/2}
           };
        data.animations = anim;
        spriteSheet = new createjs.SpriteSheet(data);
        return spriteSheet;
    };
}
/* end */


/* keyboard listener */
var last_key = 0;
document.onkeypress = function(e) {last_key = e;};
/* end */

        



/* class mob */
/*mob()-->char()-->load_spriteSheet()*/
function mob(str,x,y) {
    var spriteSheet = new load_spriteSheet(str, 32, 32);
    while(!spriteSheet){/*wait until the spriteSheet is loaded*/}
    var animations = {
                run_left:{frames: [3,5], next:"stand_left", frequency:2},
                stand_left:4,
                run_right:{frames: [6,8], next:"stand_right", frequency:2},
                stand_right:7,
                run_up:{frames: [9,11], next:"stand_up", frequency:2},
                stand_up:10,
                run_down:{frames: [0,2], next:"stand_down", frequency:2},
                stand_down:1
    };
    var character = new char(spriteSheet.init(animations), x, y); /*en el metodo init() del spriteSheet le añadimos las animaciones*/    
    return character;
}
/* end */



/* class move_listener */
function move_listener(target,up,down,left,right){
var Ltarget = target;
var Lup = up;
var Ldown = down;
var Lleft = left;
var Lright = right;


this.move = function(e) {
    if(String.fromCharCode(e.keyCode) === Lup){
        Ltarget.animation('run_up');
        Ltarget.move(Ltarget.get_x(),Ltarget.get_y()-10);
    }
    if(String.fromCharCode(e.keyCode) === Ldown){
        Ltarget.animation('run_down');
        Ltarget.move(Ltarget.get_x(),Ltarget.get_y()+10);
    }
    if(String.fromCharCode(e.keyCode) === Lleft){
        Ltarget.animation('run_left');
        Ltarget.move(Ltarget.get_x()-10,Ltarget.get_y());
    }
    if(String.fromCharCode(e.keyCode) === Lright){
        Ltarget.animation('run_right');
        Ltarget.move(Ltarget.get_x()+10,Ltarget.get_y());
    }
    last_key = 0;
};
return this;
}
/* end */